const shell = require("shelljs");
const chalk = require("chalk");
const log = console.log;
module.exports = {
  // handleCommand.js 远端执行命令
  // run linux shell(ssh对象、shell指令、执行路径)
  runCommand(ssh, command, path) {
    return new Promise((resolve, reject) => {
      ssh
        .execCommand(command, {
          cwd: path,
        })
        .then((res) => {
          if (res.stderr) {
            reject(log(chalk.red("命令执行发生错误:" + res.stderr)));
            process.exit();
          } else {
            resolve(log(chalk.green(command + " 执行完成！")));
          }
        });
    });
  },
  // 运行本地命令
  runScript(scriptSrt) {
    log(chalk.blue(`开始执行:${scriptSrt}...`));

    return new Promise((resolve) => {
      if (shell.exec(scriptSrt).code !== 0) {
        log(chalk.red(`Error: ${scriptSrt}脚本执行失败...`));
        shell.exit(1);
      } else {
        log(chalk.green(`脚本${scriptSrt}执行完成...`));
        resolve();
      }
    });
  },
};
